package com.hontech.bread.ui.dialog

import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.ProgressBar
import android.widget.TextView
import com.hontech.bread.R
import com.hontech.bread.api.uploadOrder
import com.hontech.bread.app.AppContext
import com.hontech.bread.device.deliveryAny
import com.hontech.bread.store.Order
import com.hontech.bread.store.StoreCargo
import com.hontech.bread.store.StoreProduct
import com.hontech.bread.store.setProductImage
import com.hontech.bread.store.writeError
import com.hontech.bread.ui.BaseActivity
import com.hontech.bread.utils.PayManager
import com.hontech.bread.utils.execIgnore
import com.hontech.bread.utils.runOnUiCatchAny
import kotlinx.coroutines.delay

class DeliveryDialog(private val product: StoreProduct, val cargo: StoreCargo) {

    private val view = LayoutInflater.from(AppContext).inflate(R.layout.dialog_delivery, null)
    private val window = PopupWindow(
        view,
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT,
        false
    )

    private val mImage = view.findViewById<ImageView>(R.id.id_delivery_image)
    private val mTvName = view.findViewById<TextView>(R.id.id_delivery_tv_name)
    private val mProg = view.findViewById<ProgressBar>(R.id.id_delivery_prog)
    private val mImageState = view.findViewById<ImageView>(R.id.id_delivery_state_image)
    private val mTvInfo = view.findViewById<TextView>(R.id.id_delivery_tv_info)


    init {
        window.animationStyle = R.style.PopupTheme
        mImage.setProductImage(product)
        mTvName.text = product.name

        progress()
    }

    private fun success() {
        mImageState.visibility = View.VISIBLE
        mImageState.setImageResource(R.drawable.ic_succ)
        mProg.visibility = View.GONE
        mTvInfo.text = "Shipment successful! thanks!"
    }

    private fun error() {
        mImageState.visibility = View.VISIBLE
        mImageState.setImageResource(R.drawable.ic_err)
        mProg.visibility = View.GONE
        mTvInfo.text = "Shipment failed \npayment returned to account"
    }

    private fun progress() {
        mImageState.visibility = View.GONE
        mProg.visibility = View.VISIBLE
        mTvInfo.text = "Shipping in progress \nplease wait a moment"
    }

    private suspend fun deliveryImpl() {
        deliveryAny(cargo.col, cargo.row, product.heatTime * 1000L) {}
    }

    suspend fun delivery(manager: PayManager) {
        window.showAtLocation(BaseActivity.parentView(), Gravity.CENTER, 0, 0)

        val remark = try {
            deliveryImpl()
            execIgnore { manager.success() }
            success()
            Order.REMARK_SUCC
        } catch (e: Exception) {
            e.printStackTrace()
            writeError(e)
            execIgnore { manager.fail() }
            error()
            e.message ?: Order.REMARK_FAIL
        }

        execIgnore {
            Order.create(
                product.name,
                product.productId,
                product.price,
                cargo,
                manager.getOrderPayInfo(),
                Order.TAG_OK,
                remark
            )
        }
        runOnUiCatchAny { uploadOrder() }

        delay(2000)

        window.dismiss()
    }
}



